package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcPrepaidCycle;
import com.csun.cmny.provider.model.dto.PrepaidCycleDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcPrepaidCycleService;
import com.csun.cmny.provider.validator.PrepaidCycleSaveValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/prepaidCycle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcPrepaidCycleController extends BaseController {

    @Resource
    private OmcPrepaidCycleService omcPrepaidCycleService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 保存
     * @param: [prepaidCycleDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/save")
    public Wrapper savePrepaidCycle(@Validated(PrepaidCycleSaveValidator.class) @RequestBody PrepaidCycleDto prepaidCycleDto, BindingResult br) {

        int result = omcPrepaidCycleService.savePrepaidCycle(convert(prepaidCycleDto),getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询
     * @param: [groupId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryAll")
    public Wrapper queryPrepaidCycle(@RequestBody PrepaidCycleDto prepaidCycleDto) {

        List<OmcPrepaidCycle> prepaidCycleList = omcPrepaidCycleService.queryPrepaidCycle(prepaidCycleDto,getLoginAuthDto());
        //return WrapMapper.ok(prepaidCycleList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), prepaidCycleList);
    }

    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getById/{prepaidCycleId}")
    public Wrapper getById(@PathVariable Long prepaidCycleId) {
        // 校验giftId是否为空
        Preconditions.checkArgument(prepaidCycleId != null, cmnyMessageSource
                .getMessage("gift.id.null", null));
        OmcPrepaidCycle omcPrepaidCycle = omcPrepaidCycleService.selectByKey(prepaidCycleId);
        //return WrapMapper.ok(omcPrepaidCycle);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcPrepaidCycle);
    }

    /**
     * 转换
     * @param: [prepaidCycleDto]
     * @return: com.csun.cmny.provider.model.domain.OmcPrepaidCycle
     */
    public OmcPrepaidCycle convert(PrepaidCycleDto prepaidCycleDto){
        OmcPrepaidCycle omcPrepaidCycle = new OmcPrepaidCycle();
        BeanUtils.copyProperties(prepaidCycleDto,omcPrepaidCycle);
        return omcPrepaidCycle;
    }
}


